package com.haixiaoke.mobile.house.controller;


import cn.hutool.core.util.ObjectUtil;
import com.haixiaoke.auth.AuthCheck;
import com.haixiaoke.saas.cleaner.domain.Cleaner;
import com.haixiaoke.saas.cleaner.service.ICleanerService;
import com.haixiaoke.saas.common.annotation.RepeatSubmit;
import com.haixiaoke.saas.common.annotation.TenantScope;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.common.tenant.TenantContext;
import com.haixiaoke.saas.house.domain.HouseInspection;
import com.haixiaoke.saas.house.service.IHouseInspectionService;
import com.haixiaoke.saas.system.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 验房记录Controller
 *
 * @author xiaolong
 * @date 2024-05-08
 */
@RestController
@RequestMapping("/mobile/house/inspection")
public class MobileHouseInspectionController extends BaseController {
    @Autowired
    private IHouseInspectionService houseInspectionService;
    @Autowired
    private ICleanerService cleanerService;

    /**
     * 查询验房记录列表
     */
    @GetMapping("/list")
    @TenantScope
    @AuthCheck
    public TableDataInfo list(HouseInspection houseInspection) {
        startPage();
        Long userId = getUserId();
        Cleaner cleaner = cleanerService.selectCleanerByCleanerUserId(userId, TenantContext.getTenantId());
        if (ObjectUtil.isEmpty(cleaner)) {
            throw new ServiceException("当前用户未关联保洁人员");
        }
        houseInspection.setCleanerId(cleaner.getCleanerId());
        houseInspection.setTenantId(TenantContext.getTenantId());
        List<HouseInspection> list = houseInspectionService.selectHouseInspectionList(houseInspection);
        return getDataTable(list);
    }

    /**
     * 获取验房记录详细信息
     */
    @GetMapping(value = "/{houseInspectionId}")
    @TenantScope
    @AuthCheck
    public AjaxResult getInfo(@PathVariable("houseInspectionId") String houseInspectionId) {
        return success(houseInspectionService.selectHouseInspectionByHouseInspectionId(houseInspectionId));
    }

    /**
     * 新增验房记录
     */
    @PutMapping
    @TenantScope
    @RepeatSubmit
    @AuthCheck
    public AjaxResult edit(@RequestBody HouseInspection houseInspection) {
        Long userId = getUserId();
        Cleaner cleaner = cleanerService.selectCleanerByCleanerUserId(userId, TenantContext.getTenantId());
        if (ObjectUtil.isEmpty(cleaner)) {
            throw new ServiceException("当前用户未关联保洁人员");
        }
        houseInspection.setCleanerId(cleaner.getCleanerId());
        houseInspection.setTenantId(TenantContext.getTenantId());
        houseInspection.setCleanerUserId(userId);
        houseInspection.setUpdateBy(getUsername());
        houseInspection.setOperatorId(userId);
        return toAjax(houseInspectionService.editHouseInspectionList(houseInspection));
    }


    /**
     * 验房和打扫已指派数量统计
     */
    @GetMapping(value = "/statistics")
    @TenantScope
    @AuthCheck
    public AjaxResult statistics() {
        Long userId = getUserId();
        return success(houseInspectionService.statistics(userId));
    }
}
